
PROJECT = "A9191"
VERSION = "1.1.5"
PRJID = 30
PRODUCT_KEY = 'xdNieIvozxK4lEicNH9bF0REQIlykyT5'
collectgarbage("setpause",90)
require "sys"
LOG_LEVEL = log.LOGLEVEL_TRACE
require "config"
require "nvm"
nvm.init('config.lua')

log.info('original version', nvm.get('original_version'))

if not nvm.get('original_version') then nvm.set('original_version', VERSION) end
-- 为兼容旧版本 增加fota升级统计的默认参数
if not nvm.get('fota_download_times') then nvm.set('fota_download_times', 0) nvm.set('fota_succ_times', 0) end

if nvm.get('version_before_fota') then
    local old_version = nvm.get('version_before_fota')
    nvm.set('version_before_fota', nil)
    -- 若当前版本不是更新前的版本也不是原始版本，那么判断为升级成功，记录升级成功次数
    if old_version ~= VERSION and nvm.get('original_version') ~= VERSION then
        nvm.set('fota_succ_times', nvm.get('fota_succ_times')+1)
    end
end

require "pins"
ctrl_flashlight = pins.setup(pio.P1_1, 0)

require "pm"
require "utils"
--require"console"
--console.setup()
--require"wdt"
require "display"
require "lcd"
lcd.setup()

require "ui"
require "battery"
require "audio"
require "manage"
require "welcome" -- 欢迎界面

-- 开机显示
local reason = rtos.poweron_reason()
if reason == rtos.POWERON_ALARM then
    local text = manage.getNoticeText()
    if not text then
        manage.checkNotice()
        rtos.poweroff()
    else
        manage.ringAndNotice(text, function() rtos.restart() end) -- 闹钟结束以后重启不然注册不上GSM协议栈
    end
elseif reason == rtos.POWERON_CHARGER then
    require "charging"
    ui.enter(charging())
elseif reason == rtos.POWERON_KEY then
    ui.enter(welcome('poweron'))
else
    reason = 'silenton'
end

---- 加载其他任务
require "ril"
require "sim"
require "net"
net.startQueryAll(8000, 60000)
require "cc"
require "update"
update.run()
require "ntp"
ntp.timeSync()

---- 加载界面代码
require "popup"
require "idle" -- 待机界面
require "clock"  -- 锁屏时钟
require "mainmenu"
require "dial"
require "contact"
require "wechat"
require "camera"
require "toolbox"
require "minigame"
require "quickmath"
require "english100"
require "hanzi140"
require "snake"
require "battlecity"
require "brains"
require "tetris"
require "deviceinfo"
require "flashlight"
require "call"
require "album"
require "stopwatch"
require "theme"
require "volume_brightness"
require "functiontest"
require "misc"
require "linkair"
require "tool"
require "record"
require "gopher"
require "gopherjump"
require "twentyfour"
require "travelfrog"
record.delete() -- 开机以后清除之前的录音

if reason == 'silenton' then
    ui.enter(idle())
    lcd.off()
end

function printSystemStatus()
    for _, v in ipairs(coroutine.errors) do
        log.info('coroutine.errors', v)
    end
    log.info('printSystemStatus', 'pm.isleep', pm.isleep())
    log.info('printSystemStatus', 'link.isReady', link.isReady())
    socket.printStatus()
    ril.request('AT+CPIN?')
    ril.request('AT+CREG?')
    ril.request('AT+CGATT?')
    ril.request('AT+CIPSTATUS')
end

-- 启动一个1分钟监控一次系统状态的任务,量产版屏蔽该代码
--sys.taskInit(function()
--    while true do
--        sys.wait(60000)
--        printSystemStatus()
--    end
--end)

-- 远程升级包下载完成时记录下载次数以及更新前的版本，然后重启进行升级
sys.subscribe('FOTA_DOWNLOAD_FINISH', function()
    nvm.set('fota_download_times', nvm.get('fota_download_times') + 1)
    nvm.set('version_before_fota', VERSION)
    sys.restart('fota.download.finish')
end)

sys.init(1)
sys.run()
